//
//  JYHomeViewModel.m
//  JYFarm
//
//  Created by 花花 on 2017/10/24.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYHomeViewModel.h"
#import "JYHomeBannerModel.h"
#import "JYCategorysModel.h"

@interface JYHomeViewModel()

@property (nonatomic, strong) NSMutableArray *shopcartListArray;
@end
@implementation JYHomeViewModel
//轮播图
- (void)handlerBannerDataWithAreaId:(NSString *)areaId Success:(void(^)(NSArray *lbtArr))success{

    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"areaId"] = areaId;
    NSMutableArray *arr = [NSMutableArray array];
    [HTTPRequest GET:kSlideShowUrl parameter:param success:^(id resposeObject) {
        if ([resposeObject[@"code"] integerValue] == 0) {
            for (NSDictionary *dic in resposeObject[@"data"]) {
                
                JYHomeBannerModel *model = [JYHomeBannerModel mj_objectWithKeyValues:dic];
                [arr addObject:model];
            }

            success(arr);
            
        }else{
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
    }];
}



//获取商品分类接口 分类信息包含了其子类列表
- (void)handlerCategoryDataWithSuccess:(void(^)(NSArray *resposeObject))success{

       
    [HTTPRequest GET:kCategorysUrl parameter:nil success:^(id resposeObject) {
            if ([resposeObject[@"code"] integerValue] == 0) {
            
 
            success(resposeObject[@"data"]);
        }else{
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
    }];
}




//右边数据的处理
- (void)requsetHomeRithtTableList{

    //右边数据
    [HTTPRequest POST:kFrontGoodsUrl parameter:@{@"areaId":@"d9a89941231d49b790bc2de8eb02e20f"} success:^(id resposeObject) {
        
        
        if (SUCCESS) {
            
            self.shopcartListArray = [BaseModel mj_objectArrayWithKeyValuesArray:resposeObject[@"data"][@"list"]];
            
            
            [self.delegate homeRightTableRequestDataSuccessWithArray:self.shopcartListArray];
            
        }else{
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
            
        }
        
        NSLog(@"___________%@",resposeObject);
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
    }];
    
    

    

}

//选中
- (void)selectProductAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected{


}


- (NSInteger) accountTotalCount{
    
    return 10;
    
    
    //    NSInteger totalCount = 0;
    //
    //    for (JVShopcartBrandModel *brandModel in self.shopcartListArray) {
    //        for (JVShopcartProductModel *productModel in brandModel.products) {
    //            if (productModel.isSelected) {
    //                totalCount += productModel.productQty;
    //            }
    //        }
    //    }
    //
    //    return totalCount;
}

//收藏
- (void)starProductAtIndexPath:(NSIndexPath *)indexPath{




}

//数量
- (void)changeCountAtIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count{
    
    
    BaseModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    if (count <= 0) {
        count = 1;
    } else if (count > 10000) {
        count = productModel.goodsAmount;
    }
    //根据请求结果决定是否改变数据
    productModel.goodsAmount = count;
    
    [self adduserShoppingTrolleyListIndexPath:indexPath count:count];
    
    [self.delegate shopcartAccountFortotalCount:[self accountTotalCount]];
    
}
- (void)adduserShoppingTrolleyListIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count{
    
    
    
    BaseModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    //cfdd103c-f7ac-495d-b92b-eeb67a1d1e3b
    NSDictionary *param = @{@"list":@[
                                    @{@"userId":@"cfdd103c-f7ac-495d-b92b-eeb67a1d1e3b",@"goodsId":productModel.id,@"goodsName":productModel.name,
                                      @"goodsPictureUrl":productModel.image,@"goodsPrice":@"1",@"goodsAmount":@(count)}]
                            };
    
    [HTTPRequest POST:kAdduserShoppingTrolleyUrl parameter:param success:^(id resposeObject) {
       
        
        
    } failure:^(NSError *error) {
       
    }];


    

}
- (NSMutableArray *)shopcartListArray{

    if (!_shopcartListArray) {
        _shopcartListArray = [NSMutableArray array];
    }

    return _shopcartListArray;
    
}

@end
